﻿<%@ Page language="C#" Inherits="Microsoft.SharePoint.Publishing.PublishingLayoutPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceholderID="PlaceHolderPageTitle" runat="server">
    <SharePointWebControls:FieldValue id="PageTitle" FieldName="Title" runat="server"/>
</asp:Content>

<asp:content contentplaceholderid="PlaceHolderMain" runat="server">
<div class="container-fluid intel-row-margin">
    <div class="row  text-center">
        <div>
            <div class="intel-ptc-top">
                <div class="intel-text-color intel-text-ptc-header-top">
                    <p>
                        Executive Accelerator
                    </p>
                </div>

                <div class="intel-text-ptc-content">
                    <p>
                    	"I am thrilled to launch this pilot session of the Executive Accelerator.
                   	</p>
                    <p>
                    	This custom program is our flagship executive development solution in support
                    </p>
                    <p>
                    	of Intel’s Leader Expectations and Business Imperatives. 
                    </p>
                    <p>
                    	We look forward to all you'll experience here and all you’ll create as a result."
                    </p>
                    <p style="font-size:14px">
                    	— Deb Bubb
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="intel-background-gray container-fluid">
    <div class="intel-row-margin" style="margin-bottom: 40px;">
        <div data-bind="foreach: resource">

            <p class="intel-text-color intel-ptc-week" data-bind="text: week"></p>
            <div data-bind="foreach: days">
                <p class="intel-text-color intel-ptc-day" style="font-size: 24px" data-bind="text: day">
                </p>

                <div class="row">
                    <div class="col-sm-6">
                        <p class="intel-ptc-resource-header" data-bind="text: preworkMaterials().length > 0 ? ('PRE-WORK MATERIALS') : ('PRE-WORK MATERIALS - N/A')">

                        </p>
                        <div data-bind="foreach: preworkMaterials">
                            <a class=" intel-ptc-resource" data-bind="text: title, attr: { href: url, title: title }">
                            </a>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <p class="intel-ptc-resource-header" data-bind="text: classPresentations().length > 0 ? ('CLASS PRESENTATIONS') : ('CLASS PRESENTATIONS - N/A')">

                        </p>
                        <div data-bind="foreach: classPresentations">
                            <a class=" intel-ptc-resource" data-bind="text: title, attr: { href: url, title: title }"></a>
                        </div>
                    </div>

                </div>
                <!-- ko if: $index() < ($parent.days().length - 1) --> <hr/> <!-- /ko -->

            </div>
            <!-- ko if: $index() < (resource().length - 1) --> <hr/> <!-- /ko -->
        </div>


    </div>
</div>
<script type="text/javascript">

    $(function () {

        loadData();

    });

    function loadData() {
        var consultantsList = [];
        var list = [];
        var Resource = function (week) {
            this.week = week;
            this.days = ko.observableArray();
            this.addDay = function (r) {
                this.days.push(r);
            };
        };

        var Day = function (day) {
            this.day = day;
            this.preworkMaterials = ko.observableArray();
            this.classPresentations = ko.observableArray();
            this.addPreworkMaterial = function (resource) {
                this.preworkMaterials.push(resource);
            };
            this.addclassPresentation = function (resource) {
                this.classPresentations.push(resource);
            };
        };

        function arrayFromObject(obj) {
            var arr = [];
            for (var i in obj) {
                arr.push(obj[i]);
            }
            return arr;
        }

        function groupBy(list, fn) {
            var groups = {};
            for (var i = 0; i < list.length; i++) {
                var group = JSON.stringify(fn(list[i]));
                if (group in groups) {
                    groups[group].push(list[i]);
                } else {
                    groups[group] = [list[i]];
                }
            }
            return arrayFromObject(groups);
        }

        var viewModel = function (items) {
            this.resource = ko.observableArray(items);
        };

        //get data from SP
        SP.SOD.executeFunc("sp.js", "SP.ClientContext", sharePointListReady);
        var items;
        var listTitle = "Accelerator Resources";

        function sharePointListReady() {
            var ctx = SP.ClientContext.get_current();
            var list = ctx.get_web().get_lists().getByTitle(listTitle);
            var query = new SP.CamlQuery();
            query.ViewXml = "<View><Query></Query></View>";
            //"<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type='Number'>2</Value></Eq></Where></Query></View>";
            items = list.getItems(query);
            ctx.load(items);
            ctx.executeQueryAsync(onRequestSucceeded, onRequestFailed);
        }

        function getPartial(str) {
            var partial = "";

            var io = str.indexOf(" ");
            if (io > 0) {
                partial = str.substring(io);
                var io2 = partial.indexOf(" ");
                if (io2 > 0) {
                    partial = partial.substring(0, io2);
                }
            }

            return parseInt(partial);
        }

        function onRequestSucceeded() {
            var listItemEnumerator = items.getEnumerator();
            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                var title = oListItem.get_item("FileLeafRef");
                var week = oListItem.get_item("AcceleratorResourceWeek").toUpperCase();
                var day = oListItem.get_item("AcceleratorResourceDay");
                var dayOrder = oListItem.get_item("AcceleratorResourceDayOrder");
                var type = oListItem.get_item("AcceleratorResourceType");
                list.push({ week: week, day: day, dayOrder: dayOrder, type: type, resource: { title: title, url: "../Accelerator Resources/" + title } });

            };

            var result = groupBy(list, function (item) {
                return [item.week];
            });
            result.sort(function (a, b) {
                return getPartial(a[0].week) > getPartial(b[0].week);
            });

            for (var i = 0; i < result.length; i++) {
                var a = result[i];
                var week = new Resource(a[0].week);

                var t = groupBy(a, function (item) {
                    return item.day;
                });
                t.sort(function (a, b) {
                    return (a[0].dayOrder > b[0].dayOrder);
                });
                for (var ii = 0; ii < t.length; ii++) {
                    var j = t[ii];
                    var day = new Day(j[0].day);

                    for (var iii = 0; iii < j.length; iii++) {
                        var v = j[iii];
                        if (v.type == "Pre-work Material") {
                            day.addPreworkMaterial(v.resource);
                        } else {
                            day.addclassPresentation(v.resource);

                        }
                    };
                    week.addDay(day);
                };
                consultantsList.push(week);
            };


            //// Activates knockout.js 
            ko.applyBindings(viewModel(consultantsList));

        }

        function onRequestFailed(sender, args) {
            alert("Error: " + args.get_message());
        }
    }
</script>
</asp:Content>
